// WritePresenterImpl.java
package com.example.memorypoint.mvp.write;

import com.example.memorypoint.DateBase.DiaryDatabase;
import com.example.memorypoint.entity.Diary;

public class WritePresenterImpl implements WriteContract.WritePresenter {

    private WriteContract.WriteView view;
    private DiaryDatabase diaryDatabase;
    private int diaryId; // 当前日记 ID

    public WritePresenterImpl(WriteContract.WriteView view, DiaryDatabase diaryDatabase) {
        this.view = view;
        this.diaryDatabase = diaryDatabase;
    }

    @Override
    public void loadDiary(int diaryId) {
        this.diaryId = diaryId;
        // 从数据库中加载日记数据
        Diary diary = diaryDatabase.getDiaryById(diaryId);
        if (diary != null) {
            view.showDiary(diary);
        } else {
            view.showError("加载日记失败");
        }
    }

    @Override
    public void saveDiary(String title, String content) {
        // 保存日记内容：调用数据库更新方法
        diaryDatabase.updateDiary(diaryId, title, content);
        // 保存成功后调用回调
        view.diarySaved();
    }

    @Override
    public void detachView() {
        view = null;
    }
}
